package PHS;
import java.util.*;
import java.sql.*;
public class Doctor {
    int doctorId;
    int patientId;
    String deptName;
    ArrayList nameList=new ArrayList();
    ArrayList idList=new ArrayList();
    ArrayList startTime=new ArrayList();
    ArrayList endTime=new ArrayList();
    String url="jdbc:db2://localhOst:50000/PHSYSTEM";
    String driver="com.ibm.db2.jcc.DB2Driver";
    String user="db2admin";
    String pwd="db2admin";
    Statement stmt;
    Connection conn=null;
    PreparedStatement pstmt;
    ResultSet rs;
    public Doctor()
    {
        try{
        Class.forName(driver);
        conn=DriverManager.getConnection(url,user,pwd);
        System.out.println("Doctor Connection:"+conn);
        stmt=conn.createStatement();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public void setPatientId(int patientId)
    {
        this.patientId=patientId;
        System.out.println("Setter method for patientId:"+patientId);
    }
    public void setDeptName(String deptName)
    {
        this.deptName=deptName;
        System.out.println("setDeptnameMethod:"+this.deptName);
    }
    public void setDoctorId(int doctorId)
    {
        this.doctorId=doctorId;
        System.out.println("Setter Method For doctorId:"+this.doctorId);
    }
    public void setAvailableDoctors()
    {
        /*String query="SELECT DoctorName,StartTime,EndTime" +
                " FROM Doctor D,WorkTimings W ,Department Dept" +
                " WHERE D.deptNo=Dept.DeptNo AND D.DoctorId=W.Uid AND FLAG='YES'" +
                " AND Day=UCASE(SUBSTR(DAYNAME(CURRENT DATE),1,3)) AND Dname="+"'UCASE("+deptName+")'";*/
        String query="SELECT DoctorId,DoctorName,StartTime,EndTime FROM Doctor D,WorkTimings W ,Department dp" +
                " WHERE D.deptno=Dp.deptno AND D.DoctorId=W.Uid" +
                " AND Day=UCASE(SUBSTR(DayName(CURRENT DATE),1,3)) AND FLAG='YES' " +
                "AND Dname=UCASE('"+deptName+"')";
        System.out.println(query);
        try{
            rs=stmt.executeQuery(query);
            while(rs.next())
            {
                nameList.add(rs.getString("DoctorName"));
                idList.add(rs.getInt("DoctorId"));
                startTime.add(rs.getTime("StartTime"));
                endTime.add(rs.getTime("endTime"));
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    public int updatePatient()
    {
        String query1="INSERT INTO BillDetails (PatientId,Details,Amount,BillDate) " +
                "VALUES(?,'DOCTOR FEE',(SELECT ConsultationFee FROM Doctor WHERE DoctorId="+doctorId+"),CURRENT DATE)";
        String query="UPDATE PatientTreatmentDetails SET (DoctorId,Status)=("+this.doctorId+",'WAIT_TREATMENT') " +
                "WHERE PatientId="+patientId;
        System.out.println(query);
        int urows=0;
        try{
            conn.setAutoCommit(false);
         urows=stmt.executeUpdate(query);
         pstmt=conn.prepareStatement(query1);
         pstmt.setInt(1,patientId);
         pstmt.executeUpdate();
         conn.commit();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        return urows;
    }
    public ArrayList getNameList()
    {
        System.out.println(nameList);
        return nameList;
    }
    public ArrayList getIdList()
    {
        return idList;
    }
    public ArrayList getStartTime()
    {
        System.out.println(startTime);
        return startTime;
    }
    public ArrayList getendTime()
    {
        System.out.println(endTime);
        return endTime;
    }

}
